package com.cocotingo.snail.samples;

import java.io.InputStream;

import javax.swing.JFrame;

import com.cocotingo.snail.KeyEvent;
import com.cocotingo.snail.MouseEvent;
import com.cocotingo.snail.View;
import com.cocotingo.snail.ViewContext;
import com.cocotingo.snail.template.ResourceLoader;
import com.cocotingo.snail.template.SealedView;
import com.cocotingo.snail.template.TemplateCallback;
import com.cocotingo.snail.template.TemplateException;
import com.cocotingo.snail.template.TemplateLoader;

public class SampleMain4 {

	public static void main(String[] args) {
		JFrame mainFrame = new JFrame("View Maker Tester");
		mainFrame.setSize(600, 400);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		ViewContext context = new ViewContext(mainFrame.getContentPane());
		
		TemplateLoader loader = new TemplateLoader(context.getGraphicsConfiguration());
		try {
			loader.load("SampleTemplate1.xml", new ResourceLoader() {
				@Override
				public InputStream loadResource(String resourceName) {
					return SampleMain4.class.getResourceAsStream(resourceName);
				}
			});
			View view = loader.createView("SampleView");
			context.setRootView(view);
		} catch (TemplateException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		context.prepareContent();
		mainFrame.setVisible(true);
	}
	
	public static class MainView extends SealedView {
		
		public MainView() {
			super();
		}
		
		@TemplateCallback
		public void btnClicked(SealedView view, Object arg) {
			MouseEvent e = (MouseEvent) arg;
			System.out.println(e.getPosition(view));
		}
		
		@TemplateCallback
		public int getDefaultHeight() {
			return 40;
		}
		
		@TemplateCallback
		public void postKeyPressed(SealedView sender, Object arg) {
			KeyEvent e = (KeyEvent) arg;
			if (e.getKeyChar() == 'd') {
				getViewContext().setDebugMode(true);
			}
		}
		
		@TemplateCallback
		public void postMousePressed(SealedView sender, Object arg) {
			requestKeyboardFocus();
		}
		
	}

}
